package cm.tl.pms.util.fileParser.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class PdfToExcel2 {

    public static void main(String[] args) throws IOException {
        File pdfFile = new File("C:\\Users\\Lenovo\\Documents\\WeChat Files\\wxid_2tswj262d0ye22\\FileStorage\\File\\2024-11\\测试用财务报表\\0912中化现代农业有限公司.pdf");
        // 读取PDF文件
        PDDocument document = PDDocument.load(pdfFile);
        PDFTextStripper pdfStripper = new PDFTextStripper();
        String text = pdfStripper.getText(document);

        // 创建Excel文件
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // 将PDF文本按行分割
        String[] lines = text.split("\\r?\\n");

        // 将PDF文本逐行写入Excel文件
        int rowNum = 0;
        for (String line : lines) {
            Row row = sheet.createRow(rowNum++);
            String[] cells = line.split("	");
            int cellNum = 0;
            for (String cell : cells) {
                Cell excelCell = row.createCell(cellNum++);
                excelCell.setCellValue(cell);
            }
        }

        // 保存Excel文件
//		FileOutputStream outputStream = new FileOutputStream("D:\\test\\output.xls");
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Lenovo\\Documents\\WeChat Files\\wxid_2tswj262d0ye22\\FileStorage\\File\\2024-11\\测试用财务报表\\output.xls");
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();

        // 关闭PDF文件
        document.close();
    }

}
